
package com.insigma.business.qrcode.soldier.service.impl;

import com.google.common.collect.Lists;

import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.soldier.dto.SoldierBSearcher;
import com.insigma.business.qrcode.soldier.dto.SoldierDTO;
import com.insigma.business.qrcode.soldier.entity.SoldierDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.request.SoldierInsertRequest;
import com.insigma.business.qrcode.soldier.request.SoldierSearchRequest;
import com.insigma.business.qrcode.soldier.request.SoldierUpdateRequest;
import com.insigma.business.qrcode.soldier._provider.BaseSoldierProvider;
import com.insigma.business.qrcode.soldier.service.SoldierService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service("soldierService")
public class SoldierServiceImpl implements SoldierService {

    @Resource
    @Lazy
    private BaseSoldierProvider baseSoldierProvider;
    
    @Override
    public List<SoldierDO> search(final SoldierBSearcher soldierBSearcher) {
        SoldierSearchRequest request = new SoldierSearchRequest(soldierBSearcher.getPageNo(), soldierBSearcher.getPageSize(), soldierBSearcher.getSortKeyValueJoint());
        List<SoldierDTO> dto = baseSoldierProvider.searchSoldier(request).getDto();

        List<SoldierDO> result = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(dto)) {
            for (SoldierDTO soldierDTO : dto) {
                SoldierDO  soldierDO = new SoldierDO();
                BeanUtils.copyProperties(soldierDTO, soldierDO);
                result.add(soldierDO);
            }
        }

        return result;
    }
   
    @Override
    public SoldierDO get(final String id){
        SoldierDTO dto = baseSoldierProvider.getSoldier(new BaseGetRequest(id)).getDto();
        if (dto != null) {
            SoldierDO soldierDO = new SoldierDO();
            BeanUtils.copyProperties(dto, soldierDO);
            return soldierDO;
        } else {
            return null;
        }
    }

    
    @Override
    public Integer insert(final SoldierDO soldierDO){
        SoldierInsertRequest request = new SoldierInsertRequest();
        BeanUtils.copyProperties(soldierDO, request);
        Integer dto = baseSoldierProvider.insertSoldier(request).getDto();
        return dto;
    }

    
    @Override
    public Integer update(final SoldierDO soldierDO){
        SoldierUpdateRequest updateRequest = new SoldierUpdateRequest();
        BeanUtils.copyProperties(soldierDO, updateRequest);
        Integer dto = baseSoldierProvider.updateSoldier(updateRequest).getDto();
        return dto;
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        BaseDeleteRequest request = new BaseDeleteRequest();
        request.setId(id);
        request.setModifierType(modifierType);
        request.setModifierId(modifierId);
        request.setModifyDatetime(new Date());
        Integer dto = baseSoldierProvider.deleteSoldier(request).getDto();
        return dto;
    }


}
